﻿using System.Drawing;
using System.Windows.Forms;
using Frame;
using System.Threading;
using System.IO;

namespace VideoFileOrganization
{
    public partial class BaseFrm : Form
    {
        public delegate void SenMessageEventHanle(string Msg, LogLevel l, Form frm);
        public event SenMessageEventHanle sendMessageEvent;
        private Global m_global;
        public Global global
        {
            set { m_global = value; }
            get { return m_global; }
        }
        private int index = 0;
        public int Index {
            set { index = value; } get { return index; } }

        private Image tabImage = null;
        public Image TabImage
        {
            set { tabImage = value;}
            get { return tabImage; }
        }

        private string rootPath;
        public string RootPath
        { set
            {
                rootPath = value;
                if (!Directory.Exists(rootPath)) return;
                // 在新线程中执行的代码
                var thread = new Thread(() =>
                {
                    Invoke(new UpdataUiDelegate(IniUi));
                });
                // 启动线程
                thread.Start();
            }
            get
            {
                return rootPath;
            }
        }

        private delegate void UpdataUiDelegate();
        private delegate void ApplyButtonDelegate();
        
        public BaseFrm()
        {
            InitializeComponent();
        }

        protected virtual void IniUi()
        {
            
        }

        public void OnApplyButton(int index)
        {
            if (this.index != index) return;

            Invoke(new ApplyButtonDelegate(ApplyButton));
        }

        protected virtual void ApplyButton() { }

        public void SetRootPath(string path)
        {
            RootPath = path;
        }

        public void SendMsg(string Msg, LogLevel l = LogLevel.INFO)
        {
            sendMessageEvent(Msg, l, this);
        }

        public void UpdataUi(int index)
        {
            if (!Directory.Exists(RootPath)) return;
            if (this.index == index)
            {
                Invoke(new UpdataUiDelegate(IniUi));
            }
        }

    }
}
